﻿#region 开发单位：沃哲 版权所有Copyright (C) 2011
//-------------------------------------------------------------------------------------
// 开发单位：沃哲 版权所有Copyright (C) 2011
//
// 功能描述：查询类，用于描述描述：谁 想查询什么类型、条件的数据，用来达成什么目的
//			 
// 创建标识：2011-05-05		王轲
//
// 修改标识：日	期			修改人员		修改说明
//			 
//-------------------------------------------------------------------------------------
#endregion

using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Text;

namespace TrafficArchives.Common
{
    /// <summary>
    /// 查询条件
    /// </summary>
    [DataContract]
    public class Request
    {
        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        public Request()
        {
            Items = new List<Condition>();
            Pagination = new Pagination();
            SubRequests = new List<Request>();
        }
        #endregion

        #region 属性

        /// <summary>
        /// 谁：查询操作人
        /// </summary>
        [DataMember]
        public string Who
        {
            get;
            set;
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        [DataMember]
        public List<Condition> Items
        {
            get;
            set;
        }

        /// <summary>
        /// 分页
        /// </summary>
        [DataMember]
        public Pagination Pagination
        {
            get;
            set;
        }

        /// <summary>
        /// 子查询条件
        /// </summary>
        [DataMember]
        public List<Request> SubRequests
        {
            get;
            set;
        }
        #endregion

        #region 方法

        /// <summary>
        /// 将查询条件转换为实体框架可执行Sql
        /// </summary>
        /// <returns></returns>
        public string ToSql()
        {
            IEnumerable<Condition> items = Items;
            if (items == null) return string.Empty;

            var sb = new StringBuilder();
            sb.Append("1 = 1 ");

            foreach (var condition in items)
            {
                string relation = condition.Relation.ToString();

                sb.Append(string.Format(" {0} {1} {2} {3}", relation, condition.FieldName,
                                        condition.GetOperator(), condition.GetValues()));
            }

            return sb.ToString();
        }
        #endregion

       

    }
}
